package org.util.common;

import java.lang.reflect.Field;

/**
 * 主要有一些常用的反射机制的方法
 *
 * Created by sinceow on 2014/6/16.
 */
public class ReflectUtil {

    /**
     * 获取当前target的某一属性值
     * @param target
     * @param fieldName
     * @return
     */
    public static <T> T getFieldValue(Object target, String fieldName){
        if(target == null){
            return null;
        }
        try {
            Field field = target.getClass().getDeclaredField(fieldName);
            boolean flag = field.isAccessible();
            field.setAccessible(true);
            Object obj = field.get(target);
            field.setAccessible(flag);
            return (T)obj;
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}
